import Taro from "@tarojs/taro";
// import _ from 'lodash'

export const show = (msg, duration = 3000) => {
  if (msg) {
    Taro.showToast({
      title: msg,
      icon: "none",
      duration,
    });
  } else {
    console.warn("弹窗失败，msg为空");
  }
};

export const showLong = (msg) => {
  if (msg) {
    Taro.showToast({
      title: msg,
      icon: "none",
      duration: 5000,
    });
  } else {
    console.warn("弹窗失败，msg为空");
  }
};

export const showModal = (options) => {
  Taro.showModal(options);
};

export const navigateTo = (url, options) => {
  Taro.navigateTo({ url, ...options });
};

export const redirectTo = (url, options) => {
  Taro.redirectTo({ url, ...options });
};

export const reLaunch = (url, options) => {
  Taro.reLaunch({ url, ...options });
};

export const switchTab = (url, options) => {
  Taro.switchTab({ url, ...options });
};

export const setTitle = (title) => {
  Taro.setNavigationBarTitle({ title });
};

export const navigateBack = (options) => {
  Taro.navigateBack({
    ...options,
    fail: () => {
      Taro.switchTab({
        url: "/pages/index/index",
      });
    },
  });
};

export const navigateIndex = () => {
  Taro.switchTab({
    url: "/pages/index/index",
  });
};

export const showErrorModal = (content) => {
  if (content) {
    Taro.showModal({
      title: "错误提示",
      confirmText: "知道了",
      content,
      showCancel: false,
    });
  }
};

export function setStorageSync(key, data) {
  Taro.setStorageSync(key, data);
}

export function getStorageSync(key) {
  return Taro.getStorageSync(key);
}

export function removeStorageSync(key) {
  Taro.removeStorageSync(key);
}

// export function debounce(func, wait = 1000, options = { leading: false, trailing: true }) {
// 	return _.debounce(func, wait, options)
// }
